import common from '../../utils/common.js'
import cookie from '../../utils/js-cookie'
import resetData from '../../utils/resetData.js'
import verComplie from '../../assets/lib/ver-complie-lib.js'
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    contentList: [{
      title: '功能',
      lists: [{
        tit: '同步微信信息',
        icon: '../../assets/images/icon-7.png',
        num: '',
        navTo: '../accredit/index',
        webView: ''
      }]
    }, {
      title: '图书讯息',
      lists: [{
        tit: '我的借入',
        icon: '../../assets/images/icon-1.png',
        num: '',
        navTo: '../myborrow/index',
        webView: ''
      }, {
        tit: '我的借出',
        icon: '../../assets/images/icon-2.png',
        num: '',
        navTo: '../myshare/index',
        webView: ''
      }, {
        tit: '我的图书',
        icon: '../../assets/images/icon-3.png',
        num: '',
        navTo: '../mybooks/index',
        webView: ''
      }, {
        tit: '想读图书',
        icon: '../../assets/images/icon-4.png',
        num: '',
        navTo: '../readbook/index',
        webView: ''
      }]
    }, {
      title: '服务',
      lists: [{
        tit: '我的专属客服',
        icon: '../../assets/images/icon-5.png',
        navTo: '',
        contact: true,
        webView: ''
      },
      // {
      //   tit: '常见问题',
      //   icon: '../../assets/images/icon-6.png',
      //   navTo: '../questions/index',
      //   webView: 'https://www.pertech.cn/Commonproblems/'
      // }
      ]
    }],
    mask1_show: false,
    sing_state: false,
    book_currency: '',
    $toast: {
      show: false,
      text: ''
    }
  },
  bindTapRouter(e) {
    let router = e.currentTarget.dataset.navto;
    wx.navigateTo({
      url: `${router}`
    })
  },
  onLoad(){
    console.log("当前版本号===>>>>>", verComplie)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    let self = this
    self.getUserInfo()
  },
  getUserInfo() {
    let self = this
    self.setData({
      contentList: common.deepClone(resetData.contentList)
    }, () => {
      app.getToken(token => {
        app.getApi.getUserInfo({ token: token }).then(res => {
          wx.stopPullDownRefresh();
          console.log('后端返回getUserInfo全部数据====>',res)
          console.log('后端返回getUserInfo里的data数据====>', res.data)
          if (res.code == '3000') {
            res.data.borrow_number != '' && (self.data.contentList[1].lists[0].num = res.data.borrow_number)
            res.data.share_number != '' && (self.data.contentList[1].lists[1].num = res.data.share_number)
            res.data.book_number != '' && (self.data.contentList[1].lists[2].num = res.data.book_number)
            res.data.collection_number != '' && (self.data.contentList[1].lists[3].num = res.data.collection_number)
            self.setData({
              contentList: self.data.contentList,
              userInfo: res.data
            })
            if (res.data.isaccredit == 0){
              app.getUserInfoNext()
            }
          }else{
            common.showToast(self, res.message)
          }
          // else {
          //   cookie.getCookie('fist_getuserinfo', (res) => { //首次进入我的页面，要弹出微信授权页面
              
          //   }, (res) => { //
             
          //   })
            
          // }
        }).catch(Error => {
          wx.stopPullDownRefresh();
          common.showToast(self, Error)
        })
      })
    })
  },
  updasign() {
    let self = this
    app.getToken(token => {
      app.getApi.tocreatetime({ token: token }).then(res => {
        console.log(res)
        if (res.code == 3000) {
          self.setData({
            book_currency: (res.data.book_currency || 0),
            sing_state: true,
            mask1_show: true,
          }, () => {
            self.getUserInfo()
          })
        } else {
          common.showToast(self, res.message)
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let self = this
    self.getUserInfo()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '天天享读，让图书传递爱',
      imageUrl: '../../assets/images/index_s.png',
      path: '/pages/index/index'
    }
  }
})